package com.itheima.controller;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
@Api(tags = "管理端套餐管理相关接口")
@Slf4j
public class SetmealChontroller {
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private SetmealService setmealService;

    /**
     * 图片上传
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("图片上传")
    public Result upload(MultipartFile imgFile){
        log.info("图片上传：{}",imgFile);
        try {
            //获取文件原始名称
            String filename = imgFile.getOriginalFilename();
            //获取文件后缀名
            String extension = filename.substring(filename.lastIndexOf("."));
            //构造新文件名
            String newFileName = UUID.randomUUID().toString() + extension;
            //
            String filePath = aliOssUtil.upload(imgFile.getBytes(), newFileName);
            return new Result(true,"图片上传成功",filePath);
        } catch (IOException e) {
            log.info("图片上传失败");
        }
        return new Result(false,"图片上传失败");
    }

    /**
     * 新增套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增套餐")
    public Result add(@RequestParam List<Integer> checkgroupIds ,@RequestBody Setmeal setmeal){
        log.info("新增套餐：{},{}",checkgroupIds,setmeal);
        setmealService.add(checkgroupIds,setmeal);
        return new Result(true,"新增套餐成功");
    }

    /**
     * 根据套餐id删除套餐
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    @ApiOperation("根据套餐id删除套餐")
    public Result deleteById(Integer id){
        log.info("删除套餐：{}",id);
        setmealService.deleteById(id);
        return new Result(true,"删除套餐成功");
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @ApiOperation("根据id查询套餐")
    public Result findById(Integer id){
        log.info("查询套餐：{}",id);
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true,"查询成功",setmeal);
    }

    /**
     * 编辑套餐
     * @param checkGroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("编辑套餐")
    public Result edit(@RequestParam List<Integer> checkGroupIds,@RequestBody Setmeal setmeal){
        log.info("编辑套餐：{},{}",checkGroupIds,setmeal);
        setmealService.update(checkGroupIds,setmeal);
        return new Result(true,"编辑套餐成功");
    }

    /**
     * 分页查询套餐
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation("分页查询套餐")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询套餐,{}",queryPageBean);
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }
}
